home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / cisco / account.shar / ciscotalk_vms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  3.0 KB  |  155 lines

  1. /* ciscotalk.c
  2.  *
  3.  * portable across unix/ultrix & vms systems
  4.  * first version (90/02/07) by Daniel Karrenberg (dfk@cwi.nl)
  5.  * this version (91/07/08) by Davide Salomoni (salomoni@cnaf.infn.it)
  6.  * to be linked on VMS systems with the appropriate socket library and 
  7.  * with a version of the getopt() function.
  8.  */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <netdb.h>
  13.  
  14. #ifdef VAXC
  15. #include <unixio.h>
  16. #include <types.h>
  17. #include <socket.h>
  18. #include <in.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #endif
  24.  
  25. #include "ciscotalk.h"
  26.  
  27. extern int getopt(int, char **, char *);
  28. static char *iogets(char *string,int n,int descr);
  29. static readcmd(int s);
  30.  
  31. char host[255] = SERVER;
  32. char lbuf[80];
  33. char verbose = 0;
  34.  
  35. main(int argc, char **argv)
  36. {
  37.  
  38.     extern int optind;
  39.     extern char *optarg;
  40.     int i,s;
  41.     char c;
  42.     static struct sockaddr_in sin;
  43.     struct hostent *hp,hs;
  44.     struct servent *sp;
  45.  
  46.     while ((i=getopt(argc,argv,"vh:"))!=EOF) {
  47.         switch(i) {
  48.             case 'h':
  49.                 strncpy(host,optarg,sizeof(host));
  50.                 break;
  51.             case 'v':
  52.                 verbose++;
  53.                 break;
  54.             case '?':
  55.                 fprintf(stderr,"usage: ciscotalk [-vh host]\n");
  56.                 exit(1);
  57.         }
  58.     }
  59.  
  60.     if ((hp=gethostbyname(host)) == NULL) {
  61.         fprintf(stderr, "ciscotalk: %s unknown\n", host);
  62.         exit(1);
  63.     }
  64.     hs = *hp;
  65.     /*
  66.      * hp is a pointer to a static area that could be overwritten by
  67.      * other processes or by a subsequent call to a function that uses
  68.      * the same area; it's therefore necessary to copy the data to a safe
  69.      * place.
  70.      */
  71.  
  72.     if ((s=socket(hs.h_addrtype, SOCK_STREAM, 0)) <0) {
  73.         perror("ciscotalk: cannot create socket");
  74.         exit(1);
  75.     }
  76.  
  77. #ifdef VAXC
  78. /* 
  79.  * the vms/ultrix connection does not support the getservbyname() function,
  80.  * so we define the port statically converting it first to network byte order.
  81.  */
  82.     sin.sin_port = htons(23);
  83. #else
  84.     if ((sp=getservbyname("telnet", "tcp")) == NULL)
  85.     {
  86.         fprintf(stderr, "ciscotalk: can't find service\n");
  87.         exit(1);
  88.     }
  89.     sin.sin_port = sp->s_port;
  90. #endif
  91.  
  92.     sin.sin_family = hs.h_addrtype;
  93.     sin.sin_addr = * ((struct in_addr *) hs.h_addr);
  94.  
  95.     if (verbose)
  96.         fprintf(stderr, "connecting to %s ...\n\n", hp->h_name);
  97.  
  98.     if (connect(s, &sin, sizeof (sin))) {
  99.         perror("ciscotalk: connect");
  100.         exit(1);
  101.     }
  102.  
  103.     sleep(1);
  104.     write(s, "\r\n", 2);
  105.     readcmd(s);
  106.     while (read(0,&c,1) ==1) {
  107.         if (c=='\n') {
  108.             write(s,"\r\n",2);
  109.             readcmd(s);
  110.         }
  111.         else
  112.             write(s, &c, 1);
  113.     }
  114.     exit(0);
  115.  
  116. }
  117.  
  118.  
  119. /*
  120.  * read a line from the socket using the low-level i/o, since the vms/ultrix
  121.  * connection does not support standard i/o with sockets.
  122.  */
  123. static readcmd(int s)
  124. {
  125.     while (iogets(lbuf,sizeof(lbuf)-1,s)) {
  126.         fputs(lbuf,stdout);
  127.         if (strstr(lbuf,PROMPT1) || 
  128.             strstr(lbuf,PROMPT2) || 
  129.             strstr(lbuf,PROMPT3)) {
  130.             fflush(stdout);
  131.             return;
  132.         }
  133.     }
  134.     fflush(stdout);
  135. }
  136.  
  137.  
  138. /* iogets() works like fgets() but uses low-level i/o functions. */
  139. static char *iogets(char *string,int n,int descr)
  140. {
  141.     char ch;
  142.     int i;
  143.  
  144.     if (!n) return((string=NULL));
  145.     i=0;
  146.     while (read(descr,&ch,1)==1) {
  147.         string[i]=ch;
  148.         if (++i==n || ch=='\n') {
  149.             string[i]='\0';
  150.             return(string);
  151.         }
  152.     }
  153.     return(NULL);
  154. }
  155.